<template>
  <div class="login-container">
    <div class="login-box">
<!--      login区域-->
      <div class="logo">
        <img src="../assets/logo.png" alt="">
      </div>
<!--      登录表单区域-->
      <el-form ref="loginFormRef" :model="loginForm" :rules="loginRules" class="login-form">
        <!--用户名-->
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" prefix-icon="iconfont icon-user"></el-input>
        </el-form-item>
        <!--密码-->
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" show-password prefix-icon="iconfont icon-3702mima"></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="handleLogin">登录</el-button>
          <el-button type="info" @click="handleResetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'login',
  data () {
    return {
      loginForm: { username: 'admin', password: '123456' },
      loginRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在3-10位字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在6-15位字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    handleResetForm () {
      this.$refs.loginFormRef.resetFields()
    },
    handleLogin () {
      this.$refs.loginFormRef.validate(async (valid) => {
        if (!valid) return
        const { data: result } = await this.$http.post('/login', this.loginForm)
        console.log(result)
        if (result.meta.status !== 200) return this.$message.error('登录失败')
        this.$message.success('登录成功')
        /*
        * 1.将登录成功的token，保存到客户端sessionStorage中
        *   1.1项目中除了登录之外的其他登录接口，必须在登录之后才能访问
        *   1.2token只应在当前网站打开期间生效，所以将token保存在sessionStorage中
        * 2.通过编程式导航跳转到后台首页，路由地址是/home
        * */
        window.sessionStorage.setItem('token', result.data.token)
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style lang="less" scoped>
  .login-container{
    height: 100%;
    min-height: 500px;
    background: #f0f2f5 url(../assets/background.svg) no-repeat 50%;
    background-size: 100%;
  }
  .login-box{
    position: relative;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 450px;
    height: 300px;
    background: #fff;
    border-radius: 4px;
    .logo{
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      width: 130px;
      height: 130px;
      border: 1px solid #eee;
      border-radius: 50%;
      padding: 10px;
      background: #fff;
      box-shadow: 0 0 10px #ddd;
      img{
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background: #eee;
      }
    }
  }
  .login-form{
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
  }
  .btns{
    display: flex;
    justify-content: flex-end;
  }
</style>
